#! /bin/bash
set -e

# get local IP.
get_local_ip() {
    str=`ifconfig eth1 | grep inet`

    first=`echo $str | awk '{print $1}'`
    if [[ $first == 'inet:'* ]]; then
        echo `echo $first | awk -F ':' '{print $2}'`
        return 0
    fi

    second=`echo $str | awk '{print $2}'`
    if [[ $second == 'addr'* ]]; then
        echo `echo $second | awk -F ':' '{print $2}'`
        return 0
    fi

    echo `echo $str | awk '{print $2}'`
}

# get local IP.
localIp=`get_local_ip`

# make log-dir if not exists.
if [[ ! -d "${logDir}" ]];then
        mkdir ${logDir}
fi

# make module executable.
chmod +x ${moduleName}

# bring module up.
./${moduleName} \
 --v=${logV} \
 --address=${localIp} \
 --log-dir=${logDir} \
 --client-cert-dir=${clientCertDir} \
 --server-cert-dir=${serverCertDir} \
 --regdiscv=${bcsZkHost} \
 --schedregdiscv=${mesosZkHost} \
 --logtostderr=${logToStdErr} \
 > ${logDir}/${moduleName}.log 2>&1 &
